package merlin.dao;

/**
 * Class to read data from a CSV file and enter the data in the database table
 *
 */

import java.io.*;
import java.sql.*;

public class IWDataEntry {
	
	public static void main(String[] args){
		try{
			BufferedReader br = new BufferedReader(new FileReader(args[0]));
			System.out.println("Opened file " + args[0]);
			Connection con = ConnectionPool.getConnectionPool().getConnection();
			System.out.println("connected to db");
			Statement stmt = con.createStatement();
			String line = null;
			con.setAutoCommit(false);
			while((line = br.readLine())!= null){
				String arr[] = line.split(",");
				StringBuffer sql = new StringBuffer("insert into INDUSTRY_AVERAGE_DATA values(");
				for(int i=0;i<arr.length-1;i++){
					sql.append(arr[i]).append(',');
				}
				sql.append(arr[arr.length -1]);
				sql.append(')');
				
				stmt.addBatch(sql.toString());
			}
			stmt.executeBatch();
			con.commit();
			
			stmt.close();
			con.close();
			System.out.println("done");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
}
